package com.example.outofschool;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;

public class OutFragment extends Fragment {
    private MyViewModel myViewModel;
    TextView textViewAcademy, textViewId, textViewType, textViewDay, textViewName, textViewNumber, textViewMsg;
    ImageView imageViewBack;
    private ProgressDialog dialog;
    private MutableLiveData<String> liveNumber;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.out_fragment, container, false);
        textViewAcademy = view.findViewById(R.id.textViewAcademy);
        textViewId = view.findViewById(R.id.textViewId);
        textViewType = view.findViewById(R.id.textViewType);
        textViewDay = view.findViewById(R.id.textViewDay);
        textViewName = view.findViewById(R.id.textViewName);
        textViewNumber = view.findViewById(R.id.textViewNumber);
        textViewMsg = view.findViewById(R.id.textViewMsg);
        imageViewBack = view.findViewById(R.id.imageViewBack);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myViewModel = new ViewModelProvider(requireActivity()).get(MyViewModel.class);
//      加载框显示
        dialog = new ProgressDialog(requireContext());
        dialog.setMessage("数据加载ing");
        dialog.setTitle("请稍后");
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
//      actionBar返回按钮绑定方法
        imageViewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.navigate(R.id.action_outFragment_to_mainFragment);
            }
        });
//      初始化当前日期
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        textViewDay.setText(simpleDateFormat.format(date));
//      初始化出校内容
        if (myViewModel.getCrxType().equals("入校")) textViewType.setText("欢迎返校");
        else textViewType.setText("允许出校");
        textViewMsg.append(myViewModel.getLocation() + "扫码" + myViewModel.getCrxType() + "的同学");
//      初始化用户信息
        textViewId.setText(myViewModel.getUserId());
        textViewAcademy.setText(myViewModel.getUserAcademy());
        textViewName.setText(myViewModel.getUserName());
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onStart() {
        super.onStart();
//      设置statusBar颜色
        Window window = requireActivity().getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.light_purple_200));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            window.getDecorView().setSystemUiVisibility(ViewGroup.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
//      极光统计界面统计Start
        JAnalyticsInterface.onPageStart(requireActivity(), this.getClass().getCanonicalName());
//      设置出入校人数
        liveNumber = myViewModel.getLiveNumber();
        myViewModel.setNumber();
        liveNumber.observe(requireActivity(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                textViewNumber.setText(s);
                Log.d("myTAG", "onChanged: " + s);
                if (dialog.isShowing()) dialog.dismiss();
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
//      极光统计界面统计End
        JAnalyticsInterface.onPageEnd(requireActivity(), this.getClass().getCanonicalName());
    }
}
